/**
* Copyright (C) 2018-2022
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.tanzer.service.impl;

import co.yixiang.modules.tanzer.domain.Purchase;
import co.yixiang.common.service.impl.BaseServiceImpl;
import lombok.AllArgsConstructor;
import co.yixiang.dozer.service.IGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.tanzer.service.PurchaseService;
import co.yixiang.modules.tanzer.service.dto.PurchaseDto;
import co.yixiang.modules.tanzer.service.dto.PurchaseQueryCriteria;
import co.yixiang.modules.tanzer.service.mapper.PurchaseMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import co.yixiang.domain.PageResult;
/**
* @author tanzer
* @date 2024-08-04
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "purchase")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class PurchaseServiceImpl extends BaseServiceImpl<PurchaseMapper, Purchase> implements PurchaseService {

    private final IGenerator generator;

    @Override
    //@Cacheable
    public PageResult<PurchaseDto> queryAll(PurchaseQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<Purchase> page = new PageInfo<>(queryAll(criteria));
        return generator.convertPageInfo(page,PurchaseDto.class);
    }


    @Override
    //@Cacheable
    public List<Purchase> queryAll(PurchaseQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(Purchase.class, criteria));
    }


    @Override
    public void download(List<PurchaseDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PurchaseDto purchase : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("供应商名称", purchase.getSupplierName());
            map.put("类型", purchase.getSupplierType());
            map.put("联系方式", purchase.getTel());
            map.put("微信", purchase.getWechat());
            map.put("产品名称", purchase.getSupplierProductName());
            map.put("图片", purchase.getPic());
            map.put("单价", purchase.getUnitPrice());
            map.put("数量", purchase.getNum());
            map.put("运费", purchase.getFreight());
            map.put("总价", purchase.getPriceTotal());
            map.put("备注", purchase.getRemake());
            map.put("创建日期", purchase.getCreateTime());
            map.put(" updateTime",  purchase.getUpdateTime());
            map.put(" isDel",  purchase.getIsDel());
            map.put(" attribute1",  purchase.getAttribute1());
            map.put(" attribute2",  purchase.getAttribute2());
            map.put(" attribute3",  purchase.getAttribute3());
            map.put(" attribute4",  purchase.getAttribute4());
            map.put(" attribute5",  purchase.getAttribute5());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
